import { configureStore } from "@reduxjs/toolkit";
import { useSlice } from "./slice/user";
import { useCanteenSlice } from "./slice/canteen";
import { SessionStorageManager } from './../cache'

const sessionStorageManager = new SessionStorageManager()

// https://cn.redux.js.org/redux-toolkit/overview
export const store = configureStore({
  reducer: {
    user: useSlice.reducer,
    canteen: useCanteenSlice.reducer,
  },
});

/**
 * redux持久化思路
 *
 * 监听store的变化，把每次变化的值存起来
 * 当页面刷新后，从缓存里读出来重新初始化state
 */

/**
 * 监听所有store变化
 */
store.subscribe(() => {
  const state = store.getState();
  // 每次变化存进去
  sessionStorageManager.setItem('redux-user', state.user);
});
